/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.home.presenter;

import com.yiqi.liebang.entity.bo.QuestionBo;
import com.yiqi.liebang.entity.bo.SearchBo;
import com.yiqi.liebang.entity.bo.TopicBo;
import com.yiqi.liebang.entity.bo.TopicListBo;
import com.yiqi.liebang.feature.home.contract.QuestionContract;
import com.yiqi.liebang.feature.home.dependencies.question.DaggerQuestionPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class QuestionPresenter implements QuestionContract.Presenter {

    private QuestionContract.View mView;

    @Inject

    QuestionContract.Model mModel;


    public QuestionPresenter(QuestionContract.View mView) {
        this.mView = mView;
        DaggerQuestionPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void getQuestionList(String classifyId, String classify2Id, String IntelligentType, int pageIndex, int pageSize) {
        mModel.getQuestionList(new TopicListBo(classifyId, classify2Id, IntelligentType, pageIndex, pageSize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<QuestionBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<QuestionBo> couponBoPageData) {
                if (couponBoPageData != null && couponBoPageData.size() > 0) {
                    mView.showQuestionListSuccess(couponBoPageData);
                } else {
                    mView.showQuestionListEmpty();
                }
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showQuestionListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getQuestionListMore(String classifyId, String classify2Id, String IntelligentType, int pageIndex, int pageSize) {
        mModel.getQuestionList(new TopicListBo(classifyId, classify2Id, IntelligentType, pageIndex, pageSize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<QuestionBo>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<QuestionBo> couponBoPageData) {

                mView.showQuestionListMoreSuccess(couponBoPageData);
            }

            @Override
            public void onError(Throwable e) {

                mView.showQuestionListMoreError(e.getMessage());
            }

            @Override
            public void onComplete(){
            }
        });
    }

    @Override
    public void searchQuestion(String content, int pageIndex, int pageZize) {
        mModel.questionQuestion(new SearchBo(content, pageIndex, pageZize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<QuestionBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<QuestionBo> couponBoPageData) {
                if (couponBoPageData != null && couponBoPageData.size() > 0) {
                    mView.showQuestionListSuccess(couponBoPageData);
                } else {
                    mView.showQuestionListEmpty();
                }
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showQuestionListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void searchQuestionMore(String content, int pageIndex, int pageZize) {
        mModel.questionQuestion(new SearchBo(content, pageIndex, pageZize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<QuestionBo>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<QuestionBo> couponBoPageData) {

                mView.showQuestionListMoreSuccess(couponBoPageData);
            }

            @Override
            public void onError(Throwable e) {

                mView.showQuestionListMoreError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getOtherQuestion(String type, String userUid, int pageNow, int pageSize) {
        mModel.getOtherQuestionList(new SearchBo(type,userUid,pageNow, pageSize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<QuestionBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<QuestionBo> couponBoPageData) {
                if (couponBoPageData!=null&&couponBoPageData.size()>0){
                    mView.showQuestionListSuccess(couponBoPageData);
                }else {
                    mView.showQuestionListEmpty();
                }
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showQuestionListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getOtherQuestionMore(String type, String userUid, int pageNow, int pageSize) {
        mModel.getOtherQuestionList(new SearchBo(type,userUid,pageNow, pageSize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<QuestionBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(List<QuestionBo> couponBoPageData) {
                mView.showQuestionListMoreSuccess(couponBoPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.showQuestionListMoreError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }


}
